// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import tc.common.symbology.SfeatArea;
import tc.common.symbology.Style;
import tc.crase.sys.XobjectEx;

public class ViewRoot {
	static public JDesktopPane pane = new JDesktopPane();
	static public Style styleSelected = new Style("UNDEFINED");
	
	public static void initialize() {
		styleSelected.sfeats.add(new SfeatArea(Color.PINK, 0.0));
	}
	
	enum Type { DETAIL, LIST, GRAPH };
	
	static public void updateActive() {
		JInternalFrame[] frames = pane.getAllFrames();
		for (int i = 0; i < frames.length; i++) {
			Container c = frames[i].getContentPane();
			if (c instanceof ViewXObject == false) continue;
			c.repaint();
		}
	}
	
	static public Area clip(JInternalFrame exclude) {
		Area result = new Area();
		
		JInternalFrame[] frames = pane.getAllFrames();
		
		int position = pane.getPosition(exclude);
		
		AffineTransform t = null;
		
		for (int i = 0; i < frames.length; i++) {
			if (frames[i] == exclude) 
			{	Point pt = frames[i].getLocation();
				t = AffineTransform.getTranslateInstance(-pt.x, -pt.y);
			}
			else
			{	if (pane.getPosition(frames[i]) > position) continue;
				result.add(new Area(frames[i].getBounds()));
			}
		}
		
		result.transform(t);
		
		return result;
	}
	
	static public JInternalFrame createFrame(JPanel content, String title) {
		
		JInternalFrame frame = new JInternalFrame(title, true, true, true, true);
		frame.setSize(300, 400);
		frame.setLocation((int)(Math.random()*200),(int)(Math.random()*300));
		frame.setVisible(true);
		frame.getContentPane().add(content);
		pane.add(frame);
		try {
			frame.setSelected(true);
		} catch (PropertyVetoException e) {
		}
		
		return frame;
	}
	
	public static void showObject(XobjectEx xobj) {
		ViewXObject detail = new ViewXObject();
		detail.setObj(xobj);
		JInternalFrame frame = createFrame(detail.panel, "Detail");
		detail.frame = frame;
	}
}

